import { Aiui } from "./static";
import transition from "./directives/transition";
import popper from "./directives/popper";
import getProps from "./directives/getProps";
import scroll from "./directives/scroll";
import scrollWidth from "./directives/scrollWidth";
import event from "./directives/event";
import getMaxZIndex from "./directives/getMaxZIndex";
import loading from "./directives/loading";
import jsTree from "./directives/jsTree";
import device from "./directives/device";

export var class2type = {};
export var toString = class2type.toString;
/**
 * 判断参数是否是一个数组
 */
export var isArray = Array.isArray || function(object){ return object instanceof Array };
/**
 * 判断是否为数组，类数组
 * @param {Object} obj 
 */
export function isArrayLike( obj ) {
	var length = !!obj && "length" in obj && obj.length,
		type = Aiui.type( obj );
	if ( type === "function" || isWindow( obj ) ) {
		return false;
	}
	return type === "array" || length === 0 ||
		typeof length === "number" && length > 0 && ( length - 1 ) in obj;
}
/**
 * 判断是否是方法
 * @param {Object} obj 
 */
export function isFunction(obj){
    return typeof obj === "function";
}
/**
 * 判断参数是否是对象
 * @param {Object} obj 
 */
export function isObject(obj){

    return type(obj)==="object"
}
/**
 * 获取参数的类型
 * @param {Object} obj 
 */
export function type(obj) {
    if (obj == null) {
        return obj + "";
    }
    return typeof obj === "object" || typeof obj === "function"
        ? class2type[toString.call(obj)] || "object"
        : typeof obj;
}
/**
 * 循环函数
 * @param {Object} object 循环对象
 * @param {Function} callback 回调函数
 */
export function each(obj, callback) {
    var length, i = 0;
    if (isArrayLike(obj)) {
        length = obj.length;
        for ( ; i < length; i++ ) {
            if ( callback.call( obj[ i ], i, obj[ i ] ) === false ) {
                break;
            }
        }
    } else {
        for ( i in obj ) {
            if ( callback.call( obj[ i ], i, obj[ i ] ) === false ) {
                break;
            }
        }
    }
    return obj;
}

/**
 * 判断传入的参数是否为window对象
 * @param {Object} obj 
 */
export function isWindow(obj) {
    return obj != null && obj == obj.window;
}
/**
 * 给元素设置style
 * @param {Document} element 元素
 * @param {String} att 属性名称
 */
export function getStyle(element, att){
    //特性侦测
    if(window.getComputedStyle){
        //优先使用W3C规范
        return window.getComputedStyle(element)[att];
    }else{
        //针对IE9以下兼容
        return element.currentStyle[att];
    }
}

export function css(element, att ,value){
    if(typeof value ==="undefined"){
        return getStyle(element,att);
    }else if(value==''){
        element.style[att] = '';
    }else{
        element.style[att] = value;
    }
}

export var hasClass = function(obj, cls) {
    return obj.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
  };
  
export var addClass = function(obj, cls) {
    if (!hasClass(obj, cls)) obj.className += ' ' + cls;
};
  
export var removeClass = function(obj, cls) {
    if (hasClass(obj, cls)) {
      var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
      obj.className = obj.className.replace(reg, ' ');
    }
};
  
export var toggleClass = function(obj, cls) {
    if (hasClass(obj, cls)) {
      removeClass(obj, cls);
    } else {
      addClass(obj, cls);
    }
};

/**
 * 将字符串转化为dom
 * @param {String} arg 要转化的字符串
 */
export function parseDom(arg) {
    var objE = document.createElement("div");
    objE.innerHTML = arg;
    return objE.childNodes;
};

Aiui.extend({
    device:device,
    each:each,
    type:type,
    jsTree:jsTree,
    css:css,
    hasClass:hasClass,
    addClass:addClass,
    removeClass:removeClass,
    toggleClass:toggleClass,
    getProps:getProps,
    scroll:scroll,
    scrollWidth:scrollWidth,
    getMaxZIndex:getMaxZIndex,
    parseDom:parseDom,
    event:event,
    loading:loading,
    transition:transition,
    popper:popper
})

each("Boolean Number String Function Array Date RegExp Object Error Symbol".split(" "),
    function (i, name) {
        class2type["[object " + name + "]"] = name.toLowerCase();
    }
);